﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using INTEX.DataBase;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class JLTEKGBSUPP : DocumentBase
    {
        #region-------------Properties-----------------------
        string flag = "";
        DataRow dwMaster;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTEKGBSUPPData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        private string fromdndate = "", enddndate = "",fdndate="",edndate="";
        private string fromdate = "", enddate = "",fdate="",edate="";
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }
        DataRow dwEKPOS
        {
            get
            {
                if (vwEKPOS.View == null)
                    return null;
                if ((DataRowView)vwEKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS.View.CurrentItem).Row;
            }
        }
        #endregion
        public JLTEKGBSUPP()
        {
            InitializeComponent();
            this.Name = "采购量排名";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new JLTEKGBSUPPData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("FROMDNDATE", typeof(string));
            tableUI.Columns.Add("ENDDNDATE", typeof(string));
            tableUI.Columns.Add("FROMDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();                    
                    if (fdndate == "")
                    {
                        fdndate = "0";
                    }
                    if (edndate == "")
                    {
                        edndate = "99999999";
                    } 
                    if (fdate == "")
                    {
                        fdate = "20000000";
                    }
                    if (edate == "")
                    {
                        edate = "99999999";
                    }
                    dwUI["FROMDNDATE"] = fdndate;
                    dwUI["ENDDNDATE"] = edndate; 
                    dwUI["FROMDATE"] = fdate;
                    dwUI["ENDDATE"] = edate;
                    m_Data.flag = "SUPP";
                    flag = m_Data.flag;
                    setResult = m_Data.BuildJLTEKGBSUPPData(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["EKGBSUPP"];
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
                
        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new JLTEKGBSUPPData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            JLTEKGBSUPPReport m_rpt = new JLTEKGBSUPPReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.flag = "SUPP";
            flag = m_Data.flag;
            m_rpt.fdate = fromdate;
            m_rpt.edate = enddate;
            m_rpt.sfdate = fromdndate;
            m_rpt.sedate = enddndate;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }

                
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }

        private void FROM_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdndate = "";
                fromdndate = m_Data.GetDate(sender);
                fdndate = fromdndate;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddndate = "";
                enddndate = m_Data.GetDate(sender);
                edndate = enddndate;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
                edate = enddate;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
                fdate = fromdate;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnYARNQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    if (fdndate == "")
                    {
                        fdndate = "0";
                    }
                    if (edndate == "")
                    {
                        edndate = "99999999";
                    }
                    if (fdate == "")
                    {
                        fdate = "20000000";
                    }
                    if (edate == "")
                    {
                        edate = "99999999";
                    }
                    dwUI["FROMDNDATE"] = fdndate;
                    dwUI["ENDDNDATE"] = edndate;
                    dwUI["FROMDATE"] = fdate;
                    dwUI["ENDDATE"] = edate;
                    m_Data.flag = "YARN";
                    flag = m_Data.flag;
                    setResult = m_Data.BuildJLTEKGBSUPPData(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["EKGBYARN"];
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnYARNEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new JLTEKGBSUPPData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            JLTEKGBSUPPReport m_rpt = new JLTEKGBSUPPReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.flag = "YARN";
            m_rpt.fdate = fromdate;
            m_rpt.edate = enddate;
            m_rpt.sfdate = fromdndate;
            m_rpt.sedate = enddndate;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("OpenExcel");
        }
        private void btnDetail_Click(object sender, RoutedEventArgs e)
        {
            if (dwEKPOS == null)
                return;
            MessageInfo msgs = new MessageInfo();
            msgs.Where = m_Data.UserID;
            msgs.What = "采购单打印";
            msgs.Details = "采购单打印";
            m_Data.INTEXBL.CheckEvents("A901101", dwEKPOS, "采购单打印", msgs);
            msgs.Where = m_Data.UserID;
            msgs.What = "采购单入库管理";
            msgs.Details =dwEKPOS["EKPLIEFNR"].ToString();
            m_Data.INTEXBL.CheckEvents("A901102", dwEKPOS, "采购单入库管理", msgs); 
        }
        private void MasterDg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataSet SResult = null;
            try
            {
                if (fdate == "")
                {
                    fdate = "20000000";
                }
                if (edate == "")
                {
                    edate = "99999999";
                }
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwMaster = drv.Row as DataRow;
                }
                if (dwMaster == null)
                    return;
                  SResult= m_Data.QueryEKPOS(dwMaster,flag, fdate, edate);
                  SetData.ICC_EKPOS.Clear();
                  SetData.Merge(SResult);                    
               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
}
